package com.huatai.bpmn.commom.util;

import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yxy
 */
public class dateUtils extends DateUtil {

    /**
     * 返回指定格式的日期数据
     *
     * @param date
     * @param sdf
     * @return
     */
    public static String dateToStr(Date date, SimpleDateFormat sdf) {
        if (sdf == null) {
            return "";
        }
        return sdf.format(date);
    }


    /**
     * 获取今年指定月份第一天
     *
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int month) {

        Calendar calendar = Calendar.getInstance();
        if (month == 0 || "0".equals(String.valueOf(month))) {
            month = calendar.get(Calendar.MONTH) + 1;
        }

        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取今年指定月份最后一天
     *
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();

        if (month == 0 || "0".equals(String.valueOf(month))) {
            month = calendar.get(Calendar.MONTH) + 1;
        }
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = 0;
        //2月的平年瑞年天数
        if (month == 2) {
            lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }


}
